package de.mhu.mandalaui.properties;

import de.mhu.mandalaui.Component;
import de.mhu.mandalaui.Response;
import de.mhu.mandalaui.actions.Action;
import de.mhu.mandalaui.actions.InlineAction;
import de.mhu.mandalaui.actions.SequenceAction;
import de.mhu.mandalaui.attributes.AppendAttribute;
import de.mhu.mandalaui.attributes.ArrayAttribute;
import de.mhu.mandalaui.attributes.Attribute;
import de.mhu.mandalaui.attributes.NamedAttribute;
import de.mhu.mandalaui.attributes.RenderBindCall;
import de.mhu.mandalaui.text.CheckboxField;
import de.mhu.mandalaui.text.DropdownField;
import de.mhu.mandalaui.text.InputField;
import de.mhu.mandalaui.text.Text;

public class SelectProperty extends Property {

	private Text view;
	private DropdownField edit;
	private NamedAttribute dataBind = null;
	private InlineAction bind;

	public SelectProperty(String id, String value, Attribute data) {
		if (id != null) dataBind = new NamedAttribute(id);
		view = new Text("");
		view.setBindValue(dataBind);
		
		edit = new DropdownField();
		edit.setBindValue(dataBind);
		
//		bind = new InlineAction("ko.computed(function() { var arr=", edit.getOptions(), "(); if (arr) { for (var k in arr) { var v = arr[k]; if (v['id'] == ", data,"()['id']) { return v['name']; } } } return '';})\n" );
		bind = new InlineAction("ko.computed(function() {return 'aaaah';})\n" );
		bind.setFunction(false);
		
		view.setBindValue(bind);
		 
		setId(id);
		setValue(value);
	}
	
	public void render(Response response) {
		response.registerAttribute(bind);
		System.out.println("B " + bind.getId());
		super.render(response);
	}
	
	public String getValue() {
		return view.getText();
	}

	public void setValue(String value) {
		view.setText(value);
	}

	@Override
	public Component getView() {
		return view;
	}

	@Override
	public Component getEdit() {
		return edit;
	}

	@Override
	protected void doInitialize() {
		bind.setOwner(this);
		bind.doPrepare();
	}

	@Override
	protected void doDestroy() {
//		view = null;
	}

	public ArrayAttribute getOptions() {
		return edit.getOptions();
	}

	public void setOptions(ArrayAttribute options) {
		edit.setOptions(options);
	}

	public String getOptionsValue() {
		return edit.getOptionsValue();
	}

	public void setOptionsValue(String optionsValue) {
		edit.setOptionsValue(optionsValue);
	}

	public String getOptionsText() {
		return edit.getOptionsText();
	}

	public void setOptionsText(String optionsText) {
		edit.setOptionsText(optionsText);
	}
	
}
